package com.zzh.wsss.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzh.wsss.mapper.AdminOperateMapper;
import com.zzh.wsss.mapper.ApplyOperateMapper;
import com.zzh.wsss.mapper.JobOperateMapper;
import com.zzh.wsss.mapper.UserOperateMapper;
import com.zzh.wsss.pojo.Job;
import com.zzh.wsss.pojo.JobMsg;
import com.zzh.wsss.pojo.User;
import com.zzh.wsss.pojo.UserMsg;
import com.zzh.wsss.service.AdminInterfaceService;
import com.zzh.wsss.service.UserPunchClockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminInterfaceServiceImpl implements AdminInterfaceService {

    @Autowired
    private UserOperateMapper userOperateMapper;

    @Autowired
    private UserPunchClockService userPunchClockService;

    @Autowired
    private JobOperateMapper jobOperateMapper;

    @Autowired
    private ApplyOperateMapper applyOperateMapper;

    @Override
    public Map<String, Object> getUsersMsg(String currentPage, String search) {
        Map<String, Object> map= new HashMap<String,Object>();
        if(search.equals("")){
            Page<UserMsg> page = PageHelper.startPage(Integer.parseInt(currentPage), 8).doSelectPage(()-> userOperateMapper.getUsersMsg());
            List<UserMsg> result = page.getResult();
            for (int i=0;i<result.size();i++){
                UserMsg userMsg=result.get(i);
                if(userMsg.getJobId()!=null){
                    result.get(i).setTotalWorkHour((Long) userPunchClockService.countWorkHours(userMsg.getId(),
                                                            userMsg.getJobId()).get("totalWorkHour"));
                }else{
                    result.get(i).setTotalWorkHour(0l);
                }
            }
            map.put("usersMsg",result);
            map.put("totalPage",page.getPages());
        }else{
            Page<UserMsg> page = PageHelper.startPage(Integer.parseInt(currentPage), 8)
                                            .doSelectPage(()-> userOperateMapper.getGivenUsersMsg(search));
            List<UserMsg> result = page.getResult();
            for (int i=0;i<result.size();i++){
                UserMsg userMsg=result.get(i);
                if(userMsg.getJobId()!=null){
                    result.get(i).setTotalWorkHour((Long) userPunchClockService.countWorkHours(userMsg.getId(),
                                                            userMsg.getJobId()).get("totalWorkHour"));
                }else{
                    result.get(i).setTotalWorkHour(0l);
                }
            }
            map.put("usersMsg",result);
            map.put("totalPage",page.getPages());
        }

        return map;
    }

    @Override
    public Map<String, Object> getJobsMsg(String currentPage, String search) {
        Map<String, Object> map= new HashMap<String,Object>();
        if(search.equals("")){
            Page<JobMsg> page = PageHelper.startPage(Integer.parseInt(currentPage), 8)
                    .doSelectPage(()-> jobOperateMapper.getJobsMsg());
            List<JobMsg> result = page.getResult();
            for (int i=0;i<result.size();i++){
                JobMsg jobMsg=result.get(i);
                result.get(i).setUserNum(userOperateMapper.getJobUserNum(jobMsg.getId()));
                result.get(i).setUsers(userOperateMapper.getJobUserMsg(jobMsg.getId()));
            }
            map.put("jobsMsg",result);
            map.put("totalPage",page.getPages());
        }else{
            Page<JobMsg> page = PageHelper.startPage(Integer.parseInt(currentPage), 8)
                    .doSelectPage(()-> jobOperateMapper.getGivenJobsMsg(search));
            List<JobMsg> result = page.getResult();
            for (int i=0;i<result.size();i++){
                JobMsg jobMsg=result.get(i);
                result.get(i).setUserNum(userOperateMapper.getJobUserNum(jobMsg.getId()));
                result.get(i).setUsers(userOperateMapper.getJobUserMsg(jobMsg.getId()));
            }
            map.put("jobsMsg",result);
            map.put("totalPage",page.getPages());
        }

        return map;
    }

    @Override
    public Map<String, Object> getApplysMsg(String currentPage) {
        Map<String, Object> map= new HashMap<String,Object>();
        Page<JobMsg> page = PageHelper.startPage(Integer.parseInt(currentPage), 8)
                .doSelectPage(()-> applyOperateMapper.getApplysMsg());
        List<JobMsg> result = page.getResult();
        map.put("ApplysMsg",result);
        map.put("totalPage",page.getPages());
        return map;
    }

    @Override
    public Map<String, Object> addUser(String username, String password) {
        Map<String, Object> map= new HashMap<String,Object>();
        try {
            userOperateMapper.addUser(username,password);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    @Override
    public Map<String, Object> addJob(String jobName, String number, String introduce) {
        Map<String, Object> map= new HashMap<String,Object>();
        try {
            jobOperateMapper.addJob(jobName,introduce,number);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteUser(String userId) {
        Map<String, Object> map= new HashMap<String,Object>();
        userOperateMapper.deleteUser(Integer.parseInt(userId));
        map.put("result",true);
        return map;
    }

    @Override
    public Map<String, Object> deleteJob(String jobId) {
        Map<String, Object> map= new HashMap<String,Object>();
        jobOperateMapper.deleteJob(Integer.parseInt(jobId));
        List<String> s=userOperateMapper.getJobUserMsg(Integer.parseInt(jobId));
        for (int i=0;i<s.size();i++){
            userOperateMapper.updataUserJobNull(s.get(i));
        }
        map.put("result",true);
        return map;
    }

    @Override
    public Map<String, Object> updataUserMsg(String username, String password, String jobId) {
        Map<String, Object> map= new HashMap<String,Object>();

        map.put("result","");
        if(!password.equals("")){
            userOperateMapper.SetUserPassword(username,password);
            map.put("result","密码修改成功");
        }
        if(jobId!=null){
            if(!jobId.equals("")) {
                try {
                    Job job=jobOperateMapper.getJob(Integer.parseInt(jobId));
                    job.getId();
                    userOperateMapper.updataUserJob(username,Integer.parseInt(jobId));
                    map.put("result",map.get("result")+" 换岗成功");
                }catch (Exception e){
                    map.put("result",map.get("result")+" 岗位不存在");
                }
            }else{
                userOperateMapper.updataUserJobNull(username);
                map.put("result",map.get("result")+" 离岗成功");
            }
        }

        return map;
    }

    @Override
    public Map<String, Object> updataJobMsg(String jobId, String jobName, String number, String introduce) {
        Map<String, Object> map= new HashMap<String,Object>();
        Job job=jobOperateMapper.getJob(Integer.parseInt(jobId));
        map.put("result","");
        if(!jobName.equals("")&&!job.getJobName().equals(jobName)){
            jobOperateMapper.updataJobName(Integer.parseInt(jobId),jobName);
            map.put("result","  工作名修改成功");
        }else if(jobName.equals("")){
            map.put("result",map.get("result")+"  工作名不能为空");
        }

        if(introduce!=null){
            if(job.getIntroduce()==null||!job.getIntroduce().equals(introduce)){
                jobOperateMapper.updataJobIntroduce(Integer.parseInt(jobId),introduce);
                map.put("result",map.get("result")+"  介绍修改成功");
            }
        }

        if(!number.equals("")&&job.getNumber()!=Integer.parseInt(number)){
            Integer userNum=userOperateMapper.getJobUserNum(Integer.parseInt(jobId));
            if(userNum<=Integer.parseInt(number)){
                jobOperateMapper.updataJobNumber(Integer.parseInt(jobId),Integer.parseInt(number));
                map.put("result",map.get("result")+"  岗位数修改成功");
            }else {
                map.put("result",map.get("result")+"  岗位数小于在岗人数");
            }
        }

        return map;
    }

    @Override
    public Map<String, Object> applyProcess(String applyId, String results) {
        Map<String, Object> map= new HashMap<String,Object>();
        applyOperateMapper.applyProcess(Integer.parseInt(applyId),Integer.parseInt(results));
        map.put("result",true);
        return map;
    }
}
